package com.iot.web.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iot.core.bean.TransactionBean;
import com.iot.core.model.Transaction;
import com.iot.core.service.TransactionService;
import com.iot.web.form.TransactionForm;
import com.witsight.platform.annotation.logging.Logging;
import com.witsight.platform.exception.BusinessException;
import com.witsight.platform.model.BoEntity;

/**
 * 
 * @author penght
 *
 */
@RestController
@RequestMapping("/transaction")
@Logging("transaction manage restful api.")
public class TransactionController extends BaseController {
	//productCategory
	private final static String productCategory = "productCategory";	
	//productCategory
	private final static String resolution = "resolution";
	//POS
	private final static String POS = "pos";
	//用户服务层
	@Autowired
	TransactionService transactionService;
	/**
	 * 
	 * 说明：是否开发环境
	 * @Title: refreshCache
	 * @See: {@link #refreshCache()}
	 * @author penght
	 * @return 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @date: 2018年1月9日 下午2:02:08
	 */
	@RequestMapping("/save")
	public Boolean saveTransation(@RequestBody TransactionForm transactionForm) throws IllegalAccessException, InvocationTargetException{
		Transaction transaction = transactionForm.getModel();
		if(transaction == null) {
			return false;
		}
		TransactionBean bean = new TransactionBean();
		bean.setId(transaction.getId());
		List<Integer> issueCatalogs = new ArrayList<>();
		bean.setIssueCatalogs(issueCatalogs);
		bean.settLIssuecatalogs(transactionForm.getIssuecatalogs());
		// do other things here
		bean.setCustomerInitial(transaction.getCustomerInitial());
		transactionService.Save(bean);
		return true;
	}
	/**
	 * 
	 * 说明：是否开发环境
	 * @Title: refreshCache
	 * @See: {@link #refreshCache()}
	 * @author penght
	 * @return 
	 * @throws BusinessException 
	 * @date: 2018年1月9日 下午2:02:08
	 */
	@RequestMapping("/search")
	public List<Transaction> searchTransation(@RequestBody BoEntity transactionForm) throws BusinessException{
		if(!transactionForm.containsKey(productCategory) &&
				!(transactionForm.containsKey(resolution) && transactionForm.containsKey(POS)))
			throw new BusinessException("system.search.filter.error");
		String searchKey = "transaction.findTransactionByPR";//默认查findTransactionByPR
		//如果有productCategory参数,则查findTransactionByPS
		if(transactionForm.containsKey(productCategory)) {
			searchKey = "transaction.findTransactionByPS";
		}
		List<Transaction> transactions = transactionService.findSqlAllByParams(searchKey, transactionForm);
		return transactions;
	}
}
